<?php
namespace Modules\User\Library;

use Modules\Region\Entity\Block as BlockEntity;
use Phalcon\Di;

class Block
{
    public static function userList(BlockEntity $blockEntity)
    {
        $di = Di::getDefault();
        $data = $blockEntity->getData();
        $query = [
            'sort' => [],
            'limit' => $data['limit'],
            'skip' => 0,
        ];
        if (isset($data['sort'])) {
            switch ($data['sort']) {
                case 'score':
                    $query['sort'] = [
                        'score' => -1,
                    ];
                    break;
                case 'glod':
                    $query['sort'] = [
                        'glod' => -1,
                    ];
                    break;
                case 'login':
                    $query['sort'] = [
                        'loginTime' => -1,
                    ];
                    break;
                case 'created':
                    $query['sort'] = [
                        'created' => -1,
                    ];
                    break;
            }
        }
        if (isset($data['userContentModel'])) {
            $query['conditions']['contentModel'] = ['$in'=>array_values($data['userContentModel'])];
        }

        $userList = $di->getShared('entityManager')->find('user', $query);
        return [
            '#templates' => [
                'block-userList',
                'block-' . $blockEntity->getId()
            ],
            '#module' => 'user',
            'data' => $blockEntity,
            'userList' => $userList,
        ];
    }
}